<?php
//验证邮箱
function isEmail($str){
	$str = isset($str)?trim($str):'';
	$chars = "/^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,})+\$/i";
	if (strpos($str, '@') !== false && strpos($str, '.') !== false){
		if(preg_match($chars, $str)){return true;}
	}
	return false;
}



//验证数字字母
function isLetterDigital($str,$min=0,$max=0){
	$str = isset($str)?trim($str):'';
	$len = strlen($str);
	if($len<$min || ($max!==0 && $max < $len)){
		return false;
	}
	$chars = "/^[0-9a-zA-Z]+\$/i";
	if(preg_match($chars, $str)){
		return true;
	}
	return false;
}



//验证密码
function isPassword($str,$min=0,$max=0){
	$len = strlen($str);
	if($len<$min || ($max!==0 && $max < $len)){
		return false;
	}
	$chars = "/^[0-9a-zA-Z]+\$/i";
	if(preg_match($chars, $str)){
		return true;
	}
	return false;
}




//验证数字字母中文
function isLetterDigitalChinese($str,$min=0,$max=0){
	$str = isset($str)?trim($str):'';
	$len = strlen($str);
	if($len<$min || ($max!==0 && $max < $len)){
		return false;
	}
	$chars = "/^[0-9a-zA-Z\x{4e00}-\x{9fa5}]+\$/u";
	if(preg_match($chars, $str)){
		return true;
	}
	return false;
}

//验证数字字母中文空白
function isLetterDigitalChineseBlank($str,$min=0,$max=0){
	$str = isset($str)?trim($str):'';
	$len = strlen($str);
	if($len<$min || ($max!==0 && $max < $len)){
		return false;
	}
	$chars = "/^[0-9a-zA-Z\x{4e00}-\x{9fa5}\s]+\$/u";
	if(preg_match($chars, $str)){
		return true;
	}
	return false;
}


//验证数字字母下划线
function isw($str,$min=0,$max=0){
	$str = isset($str)?trim($str):'';
	$len = strlen($str);
	if($len<$min || ($max!==0 && $max < $len)){
		return false;
	}
	$chars = "/^\w+\$/i";
	if(preg_match($chars, $str)){
		return true;
	}
	return false;
}


//是否为空
function isNotEmpty($str){
	$str = isset($str)?trim($str):'';
	if($str===''){
		return false;
	}
	return true;
}

/** 价格格式  **/
function isPrice($str){
	$str = isset($str)?trim($str):'';
	$chars = "/^0$|^0\.\d{1,2}$|^[1-9]\d{0,9}$|^[1-9]\d{0,9}\.\d{1,2}?$/";
	return preg_match($chars, $str)?1:0;
}

function is_date($str){
	$date_format = 'Y-m-d';
    $str = trim($str);
    $time = strtotime($input);
    return date($date_format, $time) == $str;
}


//验证指定大小的图片
function isImg($path,$maxsize=1000000){
	if(file_exists($path)){
		$filesize=abs(filesize($path));
		if($filesize <= $maxsize){
			$type_list = array("1"=>"gif","2"=>"jpg","3"=>"png");
			$img_info = @getimagesize ($path);
			if(isset($type_list[$img_info[2]])){
				return true;
			}
		}
	}
	return false;
}


function isCharNum($str){
	$str = isset($str)?trim($str):'';
	$chars = "/^[0-9a-zA-Z]+$/";
	return preg_match($chars, $str)?1:0;
}
function isZip($str){
	$str = isset($str)?trim($str):'';
	$chars = "/^[0-9]{6}$/";
	return preg_match($chars, $str)?true:false;
}
function isTel($str){
	$str = isset($str)?trim($str):'';
	$chars = "/^(\d{3}-\d{8})|(\d{4}-\d{7})$/";
	return preg_match($chars, $str)?true:false;
}

function isUrl($url){
	if(!filter_var($url, FILTER_VALIDATE_URL)){
		return false;
	}else{
		return true;
	}
}


function isMobile($mobile){
	$mobile = isset($mobile)?trim($mobile):'';
	$reg = "/^(13|15|18|14)[0-9]{9}$/";
	return preg_match($reg, $mobile)?true:false;
}

function isUserName($name){
	$name = isset($name)?trim($name):'';
	$reg = "/^[A-Za-z0-9_]{4,20}$/";
	return preg_match($reg, $name)?true:false;
}

/* 检查是否含有非法字符*/
function isForbid($str){
	$temp_str=trim($str);
	$temp_str = str_replace('*',"@",$temp_str);
	$temp_str = str_replace('--',"@",$temp_str);
	$temp_str = str_replace('/',"@",$temp_str);
	$temp_str = str_replace('+',"@",$temp_str);
	$temp_str = str_replace('\'',"@",$temp_str);
	$temp_str = str_replace('\\',"@",$temp_str);
	$temp_str = str_replace('$',"@",$temp_str);
	$temp_str = str_replace('^',"@",$temp_str);
	$temp_str = str_replace('.',"@",$temp_str);
	$temp_str = str_replace(';',"@",$temp_str);
	$temp_str = str_replace('<',"@",$temp_str);
	$temp_str = str_replace('>',"@",$temp_str);
	$temp_str = str_replace('"',"@",$temp_str);
	$temp_str = str_replace('=',"@",$temp_str);
	$temp_str = str_replace('{',"@",$temp_str);
	$temp_str = str_replace('}',"@",$temp_str);
	$forbid_str= '@,%,~,&';
	$forbid_array=array();
	$forbid_array = split (',',$forbid_str);
	for($i=0;$i<count($forbid_array);$i++){
		if(strpos($temp_str,$forbid_array[$i]) !== false){
			return false;
		}
	}
	return true;
}
